<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <title>天气案例</title>
</head>

<body>
    <div id="root">
        <h2>今天天气很{{info}}</h2>
        <button @click="change">切换天气</button>
        <!-- 模板不使用methos的情况  @click="可以写简单JS语句"-->
        <!-- <button @click="Hot = !Hot">切换天气</button> -->
    </div>

    <script>
        const vm = new Vue({
            el: "#root",
            data: {
                Hot: true
            },
            computed: {
                info() {
                    return this.Hot ? "炎热" : "凉爽"
                }
            },
            methods: {
                change() {
                    this.Hot = !this.Hot
                }
            },
        //    监视属性必须存在才能监视  可以监视属性和计算属性
            watch: {
                Hot: {
                    immediate: true,//初始化的时候调用handler
                    handler(newValue, oldValue) {
                        console.log("Hot被修改了", "更改后是" + newValue, "原来是" + oldValue)
                    }
                },
                info: {
                    immediate: true,//初始化的时候调用handler
                    handler(newValue, oldValue) {
                        console.log("info被修改了", "更改后是" + newValue, "原来是" + oldValue)
                    }
                }
            }
        })
        // 通过vm进行监视
        // 格式：vm.$watch("监视对象"，{监视操作})
        vm.$watch("Hot", {
            immediate: true,//初始化的时候调用handler
            handler(newValue, oldValue) {
                console.log("Hot被修改了", "更改后是" + newValue, "原来是" + oldValue)
            }
        })


    </script>

</body>

</html>